package com.swpu.service_archive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_archive.entity.DiagnoseRecord;
import com.swpu.service_archive.mapper.DiagnoseRecordMapper;
import com.swpu.service_archive.service.DiagnoseRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-19
 */
@Service
public class DiagnoseRecordServiceImpl extends ServiceImpl<DiagnoseRecordMapper, DiagnoseRecord> implements DiagnoseRecordService {

    @Override
    public void addDiagnoseData(String openid, DiagnoseRecord diagnoseRecord) {
        diagnoseRecord.setUserId(openid);
        List<String> pictureUrls = diagnoseRecord.getPictureUrls();
        List<String> fileUrls = diagnoseRecord.getFileUrls();
        String files ="";
        if(fileUrls.size()>0){
            for (String fileUrl : fileUrls) {
                files+=fileUrl+",";
            }
            files=files.substring(0,files.length()-1);
        }
        String pictures = "";
        if(pictureUrls.size()>0){
            for (String pictureUrl : pictureUrls) {
                pictures+=pictureUrl+",";
            }
            pictures= pictures.substring(0,pictures.length()-1);
        }

        diagnoseRecord.setPictures(pictures);
        diagnoseRecord.setFiles(files);
        boolean isSave = this.save(diagnoseRecord);
        if(!isSave){
            throw new RuntimeException("用户就医数据保存失败");
        }
    }

    @Override
    public List<DiagnoseRecord> getDiagnoseList(String openid) {
        LambdaQueryWrapper<DiagnoseRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiagnoseRecord::getUserId,openid);
        List<DiagnoseRecord> recordList = this.list(queryWrapper);
        return recordList;
    }

    @Override
    public DiagnoseRecord getDiagnoseDataById(String id) {

        DiagnoseRecord diagnoseRecord = this.getById(id);
        return diagnoseRecord;
    }

    @Override
    public void updateUserDiagnoseData(DiagnoseRecord diagnoseRecord) {
        List<String> pictureUrls = diagnoseRecord.getPictureUrls();
        List<String> fileUrls = diagnoseRecord.getFileUrls();
        String files ="";
        if(fileUrls.size()>0){
            for (String fileUrl : fileUrls) {
                files+=fileUrl+",";
            }
            files=files.substring(0,files.length()-1);
        }
        String pictures = "";
        if(pictureUrls.size()>0){
            for (String pictureUrl : pictureUrls) {
                pictures+=pictureUrl+",";
                System.out.println(pictureUrl);
            }
            pictures= pictures.substring(0,pictures.length()-1);
        }

        diagnoseRecord.setPictures(pictures);
        diagnoseRecord.setFiles(files);
        boolean isUpdate = this.updateById(diagnoseRecord);
        if(!isUpdate) throw new RuntimeException("更新就医信息失败");
    }

    @Override
    public int getDiagnoseRecordCount(String openid) {
        LambdaQueryWrapper<DiagnoseRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiagnoseRecord::getUserId,openid);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void deleteDiagnoseById(String id) {
        this.removeById(id);
    }
}
